package com.hsh.member.controller;

import com.hsh.member.base.Result;
import com.hsh.member.entity.Member;
import com.hsh.member.req.MemberREQ;
import com.hsh.member.service.IMemberService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @Author
 * @date2022/12/1 11:45
 */
@RestController
@RequestMapping("/member")
public class MemberController {
  Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private IMemberService memberService;


    // member/list/search/{page}/{size}
  @PostMapping("/list/search/{page}/{size}")
  public Result search(@PathVariable("page") long page,
                       @PathVariable("size") long size,
                       @RequestBody MemberREQ req){
      logger.info("查询会员列表: page={},size={}",page,size);
      return memberService.search(page,size,req);

  }

  //新增会员
    @PostMapping()
  public  Result add(@RequestBody Member member){
      boolean t = memberService.save(member);
      if(t){
          return Result.ok();
      }
      return  Result.error("新增会员失败");
  }
    @DeleteMapping("/{id}")
  public  Result delete(@PathVariable("id") int id){
        boolean b = memberService.removeById(id);
        if(b){
            return Result.ok ();
        }
        return Result.error("删除会员失败");
    }

    @GetMapping("/{id}")
    public Result get(@PathVariable int id){
        Member member = memberService.getById(id);
        return  Result.ok(member);
    }

    @PutMapping("/{id}")
    public Result update(@PathVariable int id,
                         @RequestBody Member member){
       return memberService.update(id,member);
    }
}
